import 'package:flutter/material.dart';
import 'package:logger/logger.dart';

class SwitchAndCheckBoxRoute extends StatefulWidget {
  const SwitchAndCheckBoxRoute({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => SwitchAndCheckBoxRouteState();
}

class SwitchAndCheckBoxRouteState extends State<SwitchAndCheckBoxRoute> {
  //维护单选开关状态
  bool _switchSelected = true;

  final logger = Logger(printer: PrettyPrinter());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("单选开关和复选框")),
      body: Center(
        child: Column(
          children: [
            Row(
              children: [
                Switch(
                  value: _switchSelected,
                  onChanged: (value) {
                    setState(() {
                      _switchSelected = value;
                    });
                  },
                ),
                const Text("开灯"),
              ],
            ),
            CheckboxGroup(
              items: [
                CheckboxItem("篮球", "1", false),
                CheckboxItem("乒乓球", "2", true),
                CheckboxItem("足球", "3", false),
              ],
              onChanged: (values) {
                logger.i(values);
              },
            )
          ],
        ),
      ),
    );
  }
}

class CheckboxGroup extends StatefulWidget {
  const CheckboxGroup({Key? key, required this.items, required this.onChanged})
      : super(key: key);

  // key : 值  value : 名称
  final List<CheckboxItem> items;

  final ValueChanged<List<String>> onChanged;

  @override
  State<StatefulWidget> createState() => CheckboxGroupState();
}

class CheckboxItem {
  String name;

  String value;

  bool checked;

  CheckboxItem(this.name, this.value, this.checked);
}

class CheckboxGroupState extends State<CheckboxGroup> {
  void _checkboxChanged(CheckboxItem item) {
    setState(() {
      item.checked = !item.checked;
    });
    if (item.checked) {
      if (!values.containsKey(item.value)) {
        values[item.value] = item.checked;
      }
    } else {
      if (values.containsKey(item.value)) {
        values.remove(item.value);
      }
    }
    widget.onChanged(values.keys.toList());
  }

  final Map<String, bool> values = {};

  @override
  Widget build(BuildContext context) {
    var list = widget.items.map((item) {
      if (item.checked) {
        values[item.value] = item.checked;
      }
      return GestureDetector(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 5),
          child: Row(
            children: <Widget>[
              Checkbox(
                value: item.checked,
                activeColor: Colors.red, //选中时的颜色
                onChanged: (value) => _checkboxChanged(item),
              ),
              Text(item.name),
            ],
          ),
        ),
        onTap: () => _checkboxChanged(item),
      );
    }).toList();
    return Row(
      children: list,
    );
  }
}
